## Installing the xfun2 package
install.packages("xfun2")Installing and Configuring R and RStudio
For the applied component of this course–including all in-class examples, problem sets, and your final projects, we will be using the statistical programming language R, the lingua franca of applied statistics and a powerful tool for analyzing data in the social sciences and beyond. Alongside Python, R is widely used for data analysis in academia, the private sector, and certain government agencies.1 The purpose of this guide is to help walk you through the steps necessary to get R installed and configured on your personal computer.
I have made arrangements to ensure the latest versions of R, RStudio, and RTools are installed on the computers in the Political Science Lab in Wooten Hall 173. Unfortunately, I cannot guarantee that recent versions of R, RStudio, or RTools are installed or available at other locations on the UNT campus. Note certain features and packages we use in this course may be unavailable under older versions of R or RStudio.
Installing the base R Language
The first step in the installation process is to download and install the base language from the CRAN website. The latest version, 4.2.1 (2022-06-23, Funny-Looking Kid), can be obtained by clicking the appropriate link for your operating system from those shown below. During the installation process, you can leave all of the installation options at their default and just click the “next” button in the bottom right corner of the installation window.
If you downloaded either of the MacOS versions above, you also need to download and install XQuartz. This step is only for MacOS users.
Installing and Configuring RStudio
After completing the previous steps, you can now proceed to installing the latest version of RStudio, an integrated development environment (IDE) which provides a wide range of features for programming in R, markdown, and numerous other popular languages such as HTML, CSS, C++, Python, Stan, and javascript to name a few. To download the latest version of RStudio, click on the link below that corresponds to your operating system. During the installation process, you can just leave things at their default settings and click through the installation windows.
Customizing and Configuring RStudio
Once you’ve installed RStudio, you can customize its appearance and the default behavior of many aspects of your R Sessions. Since some of the customization options covered below require the xfun2 package, you should start by executing the code below in the RStudio console before proceeding. Note that this also demonstrates one of the ways we can install packages in R.
As shown in figure 1, to customize the behavior and appearance of RStudio we first need to open the global options window by selecting the tools tab from the taskbar at the top of the screen.
Once we’ve opened the global options window, we can then click on the appearance tab to see the customization options for the general appearance of the RStudio IDE such as those shown in figure 2. RStudio includes several different theme options by default, though you may also download a wide variety of others depending on your tastes.2
In addition to customizing the appearance of RStudio you can also customize its default behavior by selecting the “General” tab as shown below. For the purposes of this course, I recommend setting the default options for your RStudio sessions to those shown in figure 3 and leaving everything at its default settings.
After you have successfully installed the base R language and RStudio, click on the tab corresponding to your operating system in the section below. Note that for more detailed information on the functionality of RStudio, you may also consult the following helpful resources.
R Tools and XCode
You will not be able to successfully complete the steps in the Stan installation tutorial without first installing Rtools. This is a required step for much of the code we will use throughout this course.
If you are on a Windows operating system, you will need to install Rtools 4.2 which can download by clicking on the hyperlink below. Rtools is required to install packages from source and, more importantly, anything that requires a C++ compiler, including though not limited to Stan.
Once again, during the installation process you can just leave things at their default settings, especially in the case of the installation directory since changing its default locations may result in errors during package compilation.
You will not be able to successfully complete the steps in the Stan installation tutorial without first installing Xcode. This is a required step for much of the code we will use throughout this course.
If you are on a MacOS operating system, you will need to install the Xcode developer tools. You can obtain the full MacOS development environment from the Apple AppStore using the link below. Xcode is required to install packages from source and, more importantly, anything that requires a C++ compiler, including though not limited to Stan.
However, since downloading this can be extremely time consuming given the large size of the full development tools suite an alternative option is to install a paired down version that provides the tools necessary for our purposes in this course without the overhead of the full Xcode development environment. You can install the paired down version of Xcode by running the code below in the RStudio console.3
## Run the command to install xcode-select
rstudioapi::terminalExecute(
command = "xcode-select --install"
)If you run into issues installing Xcode using the code above you may need to open RStudio with administrator privileges in order to successfully complete the installation.
Once you have successfully completed this step you can proceed to the tutorial for installing and configuring Stan and brms! For useful resources on getting familiar with R syntax see the resources below.
Resources for Learning R
-
- A series of interactive tutorials to help you become familiar with the syntax of R and the tidyverse
-
- An open source online book that provides one of the best introductions to R and the tidyverse to date. I recommend reading through the workflow chapters at a minimum since they are reasonably short and cover important basic guidelines you will need to know for this course.
Footnotes
Given the growing prevalence of R and other open-source languages in both the public and private sectors, it is thus somewhat disgraceful that many faculty and departments continue to teach quantitative research methods courses at both the undergraduate and graduate levels almost exclusively around proprietary software such as Stata and SPSS whose use is increasingly rare outside of ever shrinking parts of academia.↩︎
You can download the shipwreck theme here and import it into RStudio by clicking the “add” button shown in figure 2.↩︎
I thank Andrew Heiss for suggesting this simplified approach to installing Xcode.↩︎